// pages/subrate/subrate.js
var app=getApp();
import regeneratorRuntime from '../../utils/regenerator-runtime/runtime.js'
import {
  wxUpload,
  wxRequest
} from '../../utils/util.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
      orderInfo:null,
      imgPath:app.globalData.imgPath,
      tempFilePaths:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id;
    let self=this;
    wx.request({
      url: app.globalData.reqPath+'shop/orders/'+id,
      header:{
        'authorization': app.globalData.authorization
      },
      success:function({data}){
        let info=data.data;
        info.Items.forEach(function(item){
          item.upImgs=[]
        })
        console.log(info)
        self.setData({ orderInfo: info})
      }
    })
    wx.hideShareMenu()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upImg:function(e){//点击添加图片
    let pindex=e.currentTarget.dataset.pindex;
    let self=this;
    console.log(this.data.orderInfo.Items[pindex].upImgs.length)
      wx.chooseImage({
        count: 9 - this.data.orderInfo.Items[pindex].upImgs.length,
        sizeType: ['original', 'compressed'],
        success: function(res) {
          let tempFilePaths = res.tempFilePaths;
          console.log(tempFilePaths);
          self.setData({tempFilePaths:tempFilePaths})
          self.uploadImg(tempFilePaths,0,pindex)
        },
      })
  },
  uploadImg: async function (imgs, i, pindex){
    let files = imgs;
    let pi=pindex
       i?i:0
    let len=files.length;
    let self=this;
    let orderInfo = this.data.orderInfo
   // let upimgs = orderInfo.Items[pi].upImgs
    if(i<len){
    await wxUpload(app.globalData.reqPath + 'shop/upload', { file: files[i], 'authorization': app.globalData.authorization}).then(function(res){
      console.log(res)
      let result=JSON.parse(res)
      if (result.code==200){       
          i++;
          console.log(i)
       orderInfo.Items[pi].upImgs.push(result.data);
          self.setData({orderInfo: orderInfo})
          console.log(self.data.orderInfo)
          self.uploadImg(self.data.tempFilePaths, i, pi)
        }
      })
    }
   
  },
  delImg:function(e){
    let pindex = e.currentTarget.dataset.pindex;
    let index=e.currentTarget.dataset.index;
    let orderInfo=this.data.orderInfo;
    let self=this;
    orderInfo.Items[pindex].upImgs.splice(index,1);
    this.setData({orderInfo:orderInfo})
  },
  sub:async function(n){//发布
    this.tosub(0)
     
  },
  inputcontent:function(e){//输入评价内容
    let pindex = e.currentTarget.dataset.pindex;
    let orderInfo=this.data.orderInfo;
    orderInfo.Items[pindex].reviewContent=e.detail.value;
    this.setData({orderInfo:orderInfo});
  },
  onChange:function(e){//星级评价
    console.log(e.detail)
    let pindex = e.currentTarget.dataset.pindex;
    let orderInfo = this.data.orderInfo;
    orderInfo.Items[pindex].reviewMark= e.detail;
    this.setData({orderInfo:orderInfo});
  },
  tosub:async function(n){//发布
     n?n:0
    let Items = this.data.orderInfo.Items
    let hasmark=Items.filter(function(item){
      return item.reviewMark;
    })
    console.log(hasmark.length)
    let len = Items.length;
    if (hasmark.length==len){
      let self = this;
      console.log(this.data.orderInfo)
      console.log(n)
      if (n < len) {
        await wxRequest(app.globalData.reqPath + 'shop/GoodsComments', { 'authorization': app.globalData.authorization, method: 'POST', data: { reviewContent: Items[n].reviewContent, reviewMark: Items[n].reviewMark, imgs: Items[n].upImgs.join(), itemId: Items[n].id } }).then(function (res) {
          if (res.code == 200) {
            n++;
            self.tosub(n)
          }

        })
      } else if (n >= len) {

        wx.redirectTo({
          url: '/pages/retesuccess/retesuccess',
        })
      }
    }else{
      wx.showToast({
        title: '请评价商品',
        icon:'none'
      })
    }
  }
})